package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

import java.util.Arrays;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/push-dominoes/">838. 推多米诺</a>
 */
public class Solution838 extends BaseSolution {

    public String pushDominoes(String dominoes) {
        int p = 0;
        boolean hasLeft = false;
        int len = dominoes.length();
        int left = 0;
        char[] rst = new char[len];
        Arrays.fill(rst, '.');
        while (p < len) {
            char c = dominoes.charAt(p++);
            if (c == 'R') {
                if (hasLeft) {
                    while (left < p) {
                        rst[left++] = 'R';
                    }
                }
                left = p - 1;
                hasLeft = true;
            } else if (c == 'L') {
                int right = p - 1;
                if (hasLeft) {
                    while (left < right) {
                        rst[left++] = 'R';
                        rst[right--] = 'L';
                    }
                } else {
                    while (right >= left) {
                        rst[right--] = 'L';
                    }
                }
                hasLeft = false;
                left = p;
            }
        }
        if (hasLeft) {
            while (left < len) {
                rst[left++] = 'R';
            }
        }
        return String.valueOf(rst);
    }

    public static void main(String[] args) {
        Solution838 solution = new Solution838();
        System.out.println(solution.pushDominoes("RR.L"));
    }
}